package com.gxwzxy.medic.until;

import java.sql.*;

/**
 * 鏁版嵁搴撹繛鎺�
 * 
 * @author lsj
 *
 */
public class Connect {
	private static String driver = "com.mysql.jdbc.Driver";
	private static String url = "jdbc:mysql://localhost:3306/medic?characterEncoding=utf-8&&useSSL=false";
	private static String user = "root";
	private static String pwd = "123456";

	private static PreparedStatement ps = null;
	private static Connection conn = null;
	private static ResultSet rs = null;
	/**
	 * 杩炴帴鏁版嵁搴撶殑鏂规硶
	 * 
	 * @return conn
	 */
	public synchronized static Connection getConnection() {
		try {
			Class.forName(driver);
			conn = DriverManager.getConnection(url, user, pwd);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
	}

	/**
	   * 鍏抽棴鏁版嵁搴撹繛鎺�
	 * @param conn
	 * @param ps
	 * @param rs
	 */
	public synchronized  static void close(Connection conn, PreparedStatement ps, ResultSet rs) {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (ps != null) {
			try {
				ps.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 浠呭叧闂袱涓殑鏂规硶
	 * 
	 * @return
	 */
	public synchronized static void closedouble(Connection conn, PreparedStatement ps) {
		Connect.close(conn, ps, null);
	}


	// 灏佽澧炲姞鍒犻櫎淇敼鐨勯�氱敤宸ュ叿鏂规硶
	/**
	 * sql SQL璇彞 objs SQL璇彞鍗犱綅绗﹀疄鍙傦紝濡傛灉娌℃湁鍙傛暟鍒欎紶鍏ull 杩斿洖澧炲垹鏀圭殑缁撴灉锛岀被鍨嬩负int
	 */
	public synchronized static int executeDML(String sql, Object... objs) {
		// 澹版槑jdbc鍙橀噺
		int i = -1;
		try {
			// 获取数据库连接
			conn = Connect.getConnection();
			// 关闭事务
			conn.setAutoCommit(false);
			// 获取sql语句的会话
			ps = conn.prepareStatement(sql);
			// 给sql语句的占位符插入相应的值
			if (objs != null) {
				for (int j = 0; j < objs.length; j++) {
					ps.setObject(j + 1, objs[j]);
				}
			}
			// 执行sql，并回滚
			i = ps.executeUpdate();
			conn.commit();
		} catch (Exception e) {
			try {
				//一定要在出现异常的时候回滚，不然会锁住数据库，在finally执行close的时候会造成数据的不一致性
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally {
			Connect.closedouble(conn, ps);
		}
		return i;
	}
	
	/**
	   * 鏌ヨ鏂规硶
	 * @param sql
	 * @param objs
	 * @return rs
	 */
	public synchronized static ResultSet executeF(String sql, Object... objs) {
		// 澹版槑jdbc鍙橀噺
		try {
			// 鑾峰彇杩炴帴瀵硅薄
			conn = Connect.getConnection();
			// 寮�鍚簨鍔＄鐞�
			conn.setAutoCommit(false);
			// 鍒涘缓sql鍛戒护琛�
			ps = conn.prepareStatement(sql);
			// 缁欏崰浣嶇璧嬪��
			if (objs != null) {
				for (int j = 0; j < objs.length; j++) {
					ps.setObject(j + 1, objs[j]);
				}
			}
			// 鎵цsql
			rs = ps.executeQuery();
			conn.commit();
		} catch (Exception e) {
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}
		return rs;
	}
}
